package com.hidtechs.popfax.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLOutboxHandler extends DefaultHandler{ 

	// =========================================================== 
	// Fields 
	// =========================================================== 
	private boolean apiresult = false;
	private boolean authentication = false;
	private boolean retrieveoutgoingfaxlist = false;
	private boolean result = false;
	private boolean fax = false;
	private boolean faxid = false;
	private boolean recipient = false;
	private boolean recipientscount = false;
	private boolean date = false;
	private boolean pages = false;
	private boolean error = false;
	
	
	private ParsedOutboxDataSet obj;
	public ArrayList<Object> arrayOfObject;
	
	//private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
	
	@Override 
	public void startDocument() throws SAXException { 
		arrayOfObject = new ArrayList<Object>(); 
		System.out.println("XMLLOgin start Doc="+arrayOfObject);
	//	this.myParsedExampleDataSet = new ParsedExampleDataSet();
	} 
	
	
//	public ParsedExampleDataSet getParsedData() { 
//		return this.myParsedExampleDataSet; 
//	}

	@Override 
	public void endDocument() throws SAXException { 
		// Nothing to do 
	} 

	/** Gets be called on opening tags like: 
	 * <tag> 
	 * Can provide attribute(s), when xml was like: 
	 * <tag attribute="attributeValue">*/ 
	@Override 
	public void startElement(String namespaceURI, String localName, 
			String qName, Attributes atts) throws SAXException { 
		if (localName.equals("API_RESULT")) { 
			System.out.println("In API_RESULT");
			obj = new ParsedOutboxDataSet();
			this.apiresult = true; 
		}else if (localName.equals("AUTHENTICATION")) {
			System.out.println("In AUTHENT");
			this.authentication = true; 
		}else if (localName.equals("RESULT") ) { 
			System.out.println("In RESULT");
			this.result = true; 
		}	else if (localName.equals("RETRIEVE_OUTGOING_FAX_LIST") ) { 
			System.out.println("In RETRIEVE_OUTGOING_FAX_LIST");
			this.retrieveoutgoingfaxlist = true; 
		}	else if (localName.equals("FAX") ) { 
			obj = new ParsedOutboxDataSet();
			System.out.println("In FAX");
			this.fax = true; 
		}	else if (localName.equals("FAX_ID") ) { 
			System.out.println("In FAX_ID");
			this.faxid = true; 
		}	else if (localName.equals("RECIPIENT") ) { 
			System.out.println("In RECIPIENT");
			this.recipient = true; 
		}	else if (localName.equals("RECIPIENTS_COUNT") ) { 
			System.out.println("In RECIPIENTS_COUNT");
			this.recipientscount = true; 
		}	else if (localName.equals("DATE") ) { 
			System.out.println("In DATE");
			this.date = true; 
		}	else if (localName.equals("PAGES") ) { 
			System.out.println("In PAGES");
			this.pages = true; 
		}	else if (localName.equals("ERROR") ) { 
			System.out.println("In ERROR");
			this.error = true; 
		}
	}
	/** Gets be called on closing tags like: 
	 * </tag> */ 
	@Override 
	public void endElement(String namespaceURI, String localName, String qName) 
	throws SAXException { 
		if (localName.equals("API_RESULT")) { 
			System.out.println("OUT API_RESULT");
			this.apiresult = false; 
		}else if (localName.equals("AUTHENTICATION")) {
			System.out.println("OUT AUTHENT");
			this.authentication = false; 
		}else if (localName.equals("RESULT") ) { 
			System.out.println("OUT RESULT");
			this.result = false;      
		}else if (localName.equals("RETRIEVE_OUTGOING_FAX_LIST") ) { 
			System.out.println("OUT RETRIEVE_OUTGOING_FAX_LIST");
			this.retrieveoutgoingfaxlist = false; 
		}else if (localName.equals("FAX") ) { 
			System.out.println("OUT FAX");
			arrayOfObject.add(obj);
			this.fax = false; 
		}else if (localName.equals("FAX_ID") ) { 
			System.out.println("OUT FAX_ID");
			this.faxid = false; 
		}else if (localName.equals("RECIPIENT") ) { 
			System.out.println("OUT RECIPIENT");
			this.recipient = false; 
		}else if (localName.equals("RECIPIENTS_COUNT") ) { 
			System.out.println("OUT RECIPIENTS_COUNT");
			this.recipientscount = false; 
		}else if (localName.equals("DATE") ) { 
			System.out.println("OUT DATE");
			this.date = false; 
		}else if (localName.equals("PAGES") ) { 
			System.out.println("OUT PAGES");
			this.pages = false; 
		}else if (localName.equals("ERROR") ) { 
			System.out.println("OUT ERROR");
			this.error = false; 
		}
	} 

	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */ 
	@Override 
	public void characters(char ch[], int start, int length) { 
		if(this.result){ 
			System.out.println("Result="+ch.toString());
			obj.setResult(Integer.parseInt((new String(ch, start, length)))); 
		}
		else if (this.faxid){ 
			System.out.println("faxid="+ch.toString());
			obj.setFaxid(Integer.parseInt((new String(ch, start, length)))); 
		}else if (this.recipient){ 
			System.out.println("recipient="+ch.toString());
			obj.setRecipient(new String(ch, start, length)); 
		}else if (this.date){ 
			System.out.println("date="+ch.toString());
			obj.setDate(new String(ch, start, length)); 
		}else if (this.pages){ 
			System.out.println("pages="+ch.toString());
			obj.setPages(new String(ch, start, length)); 
		}else if (this.error){ 
			System.out.println("error="+ch.toString());
			obj.setError(Integer.parseInt((new String(ch, start, length)))); 
		}
	} 
}